﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Jy123.Web.Module.Venues
{
    public class VenuesClubInfo : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string key = "VenuesId";
            string venuesIdString = context.Request.QueryString[key];
            string strMark = context.Request.QueryString["Mark"];
            string strClubId;
            if (!string.IsNullOrEmpty(venuesIdString))
            {

                int venuesId = int.Parse(venuesIdString);
                Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

                Model.Venues_Main venues = BLLVenues.GetModel(venuesId);
                string[] strCId = venues.RaceClub.Split(',');
                if (strCId.Length > int.Parse(strMark))
                {
                    strClubId = strCId[int.Parse(strMark)];
                    Model.Club_Main club;
                    if (!string.IsNullOrEmpty(strClubId))
                    {
                        int clubId = int.Parse(strClubId);
                        club = BLLClub.GetModel(clubId);
                        Model.User_Main user = BLLUser.GetModel(club.FUser.Value);
                        Model.Public_Level level = BLLLevel.GetModel(club.FLevel.Value);
                        string strResult = ReplaceClubInfo(club, level, user, context);
                        context.Response.Write(strResult);
                    }
                }

            }
        }

        private string ReplaceClubInfo(Jy123.Model.Club_Main club, Jy123.Model.Public_Level clubLevel, Jy123.Model.User_Main cUser, HttpContext context)
        {
            string strClub = string.Empty;
            if (club.ID > 0)
            {
                strClub = context.Cache["venuesraceclubdiv"].ToString();
                int maxLength = 80;
                int skill;
                if (club.Skill.HasValue)
                {
                    skill = club.Skill.Value + clubLevel.Skill.Value;
                }
                else
                {
                    skill = clubLevel.Skill.Value;
                }
                int skillW = skill * maxLength / clubLevel.PeakSkill.Value;
                int ph;
                if (club.PhysicalFitness.HasValue)
                {
                    ph = club.PhysicalFitness.Value + clubLevel.PhysicalFitness.Value;
                }
                else
                {
                    ph = clubLevel.PhysicalFitness.Value;
                }

                int phW = ph * maxLength / clubLevel.PeakPH.Value;
                int live;
                if (club.LiveNess.HasValue)
                {
                    live = club.LiveNess.Value + clubLevel.Liveness.Value;
                }
                else
                {
                    live = clubLevel.Liveness.Value;
                }
                int liveW = live * maxLength / clubLevel.PeakLive.Value;
                int moral;
                if (club.Morale.HasValue)
                {
                    moral = club.Morale.Value + clubLevel.Moral.Value;
                }
                else
                {
                    moral = clubLevel.Moral.Value;
                }

                int moralW = moral * maxLength / clubLevel.PeakMoral.Value;
                int exp;
                if (club.Experience.HasValue)
                {
                    exp = club.Experience.Value + clubLevel.Exp.Value;
                }
                else
                {
                    exp = clubLevel.Exp.Value;
                }

                int expW = exp * maxLength / clubLevel.PeakExp.Value;
                strClub = strClub.Replace("$SkillW$", skillW.ToString());
                strClub = strClub.Replace("$PHW$", phW.ToString());
                strClub = strClub.Replace("$LiveW$", liveW.ToString());
                strClub = strClub.Replace("$MoralW$", moralW.ToString());
                strClub = strClub.Replace("$ExpW$", expW.ToString());

                strClub = strClub.Replace("$Skill$", skill.ToString());
                strClub = strClub.Replace("$PH$", ph.ToString());
                strClub = strClub.Replace("$Live$", live.ToString());
                strClub = strClub.Replace("$Moral$", moral.ToString());
                strClub = strClub.Replace("$Exp$", exp.ToString());

                strClub = strClub.Replace("$UserName$", cUser.UserName);
                strClub = strClub.Replace("$ClubName$", club.CName);
            }

            return strClub;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
